import java.util.ArrayList;
import java.util.List;

public class Solution382 {

    public static class ListNode {
        int val;
        ListNode next;

        ListNode() {
        }

        ListNode(int val) {
            this.val = val;
        }

        ListNode(int val, ListNode next) {
            this.val = val;
            this.next = next;
        }
    }

    List<Integer> list=new ArrayList<>();

    public Solution382(ListNode head) {
        while (head!=null){
            list.add(head.val);
            head=head.next;
        }
    }

    public int getRandom() {
        int n=list.size();
        int t=(int)(Math.random()*n);
        return list.get(t);
    }

    public static void main(String[] args) {
        ListNode listNode=new ListNode(1,new ListNode(2,new ListNode(3)));
        Solution382 solution382=new Solution382(listNode);
        for (int i = 0; i < 5; i++) {
            System.out.println(solution382.getRandom());
        }
    }
}
